home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 13.1 KB | 465 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWFont.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWFONT_H
- #include "FWFont.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWPOINT_H
- #include "FWPoint.h"
- #endif
-
- #ifndef FWFXMATH_H
- #include "FWFxMath.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWRASTER_H
- #include "FWRaster.h"
- #endif
-
- #ifndef FWGDEV_H
- #include "FWGDev.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- #ifndef FWCOMMON_H
- #include "FWCommon.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _TRANSFORM_
- #include <Trnsform.xh>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
- #include <TextUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__FONTS__)
- #include <Fonts.h>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwgraphx
- #endif
-
- FW_DEFINE_CLASS_M1(FW_PFont, FW_CGraphicCountedPtr)
- FW_DEFINE_CLASS_M1(FW_CFontRep, FW_CGraphicCountedPtrRep)
-
- FW_REGISTER_ARCHIVABLE_CLASS(FW_LFontRep, FW_CFontRep, FW_CFontRep::Read, FW_CGraphicCountedPtrRep::Write)
-
- //========================================================================================
- // struct FW_SFontMetrics
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_SFontMetrics::PrivGetFontMetrics
- //----------------------------------------------------------------------------------------
-
- void FW_SFontMetrics::PrivGetFontMetrics(FW_CGraphicDevice* device, FW_SFontMetrics& fontMetrics)
- {
- #ifdef FW_BUILD_MAC
- FW_UNUSED(device);
- ::GetFontInfo(&fontMetrics);
- #endif
-
- #ifdef FW_BUILD_WIN
- HDC dc = device->GetPlatformCanvas();
- ::SetMapMode(dc, MM_ANISOTROPIC);
-
- SIZE sizeOldWinExt;
- ::SetWindowExtEx(dc, 1440, 1440, &sizeOldWinExt);
-
- SIZE sizeOldViewExt;
- ::SetViewportExtEx(dc, ::GetDeviceCaps(dc, LOGPIXELSX), ::GetDeviceCaps(dc, LOGPIXELSY), &sizeOldViewExt);
-
- ::GetTextMetrics(dc, &fontMetrics);
-
- ::SetMapMode(dc, MM_TEXT);
- ::SetWindowExtEx(dc, sizeOldWinExt.cx, sizeOldWinExt.cx, NULL);
- ::SetViewportExtEx(dc, sizeOldViewExt.cx, sizeOldViewExt.cy, NULL);
- #endif
- }
-
- //========================================================================================
- // class FW_PFont
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::FW_PFont
- //----------------------------------------------------------------------------------------
-
- FW_PFont::FW_PFont() :
- FW_CGraphicCountedPtr()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::FW_PFont
- //----------------------------------------------------------------------------------------
-
- FW_PFont::FW_PFont(const FW_CString& fontName, FW_FontStyle fontStyle, FW_CFixed fontSize) :
- FW_CGraphicCountedPtr()
- {
- SetRep(new FW_CFontRep(fontName, fontStyle, fontSize));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::FW_PFont
- //----------------------------------------------------------------------------------------
-
- FW_PFont::FW_PFont(const FW_PFont& other) :
- FW_CGraphicCountedPtr(other)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::FW_PFont
- //----------------------------------------------------------------------------------------
-
- FW_PFont::FW_PFont(FW_CFontRep* fontRep) :
- FW_CGraphicCountedPtr()
- {
- SetRep(fontRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::FW_PFont
- //----------------------------------------------------------------------------------------
-
- FW_PFont::~FW_PFont()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PFont& FW_PFont::operator=(const FW_PFont& other)
- {
- // We don't need to test this == &other because SetRep will do it
- SetRep(other.GetRep());
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PFont& FW_PFont::operator=(FW_CFontRep* fontRep)
- {
- // We don't need to test this == &other because SetRep will do it
- SetRep(fontRep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::FW_PFont
- //----------------------------------------------------------------------------------------
-
- FW_PFont::FW_PFont(FW_EStandardFonts /* std */)
- {
- SetRep(new FW_CFontRep(FW_kSystemFont, FW_kPlain, FW_IntToFixed(12)));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PFont::operator=
- //----------------------------------------------------------------------------------------
-
- FW_PFont& FW_PFont::operator=(FW_EStandardFonts /* std */)
- {
- SetRep(new FW_CFontRep(FW_kSystemFont, FW_kPlain, FW_IntToFixed(12)));
- return *this;
- }
-
- //========================================================================================
- // class FW_CFontRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::FW_CFontRep
- //----------------------------------------------------------------------------------------
-
- FW_CFontRep::FW_CFontRep() :
- FW_CGraphicCountedPtrRep(),
- fFontStyle(FW_kPlain),
- fFontSize(FW_IntToFixed(12)),
- fFontName()
- #ifdef FW_BUILD_MAC
- ,fMacValidCache(FALSE)
- #endif
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::FW_CFontRep
- //----------------------------------------------------------------------------------------
-
- FW_CFontRep::FW_CFontRep(const FW_CString& fontName, FW_FontStyle fontStyle, FW_CFixed fontSize) :
- FW_CGraphicCountedPtrRep(),
- fFontStyle(fontStyle),
- fFontSize(fontSize),
- fFontName(fontName)
- #ifdef FW_BUILD_MAC
- ,fMacValidCache(FALSE)
- #endif
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::FW_CFontRep
- //----------------------------------------------------------------------------------------
-
- FW_CFontRep::FW_CFontRep(const FW_CFontRep& otherRep):
- FW_CGraphicCountedPtrRep(),
- fFontStyle(otherRep.fFontStyle),
- fFontSize(otherRep.fFontSize),
- fFontName(otherRep.fFontName)
- #ifdef FW_BUILD_MAC
- ,fMacValidCache(FALSE)
- #endif
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::FW_CFontRep
- //----------------------------------------------------------------------------------------
-
- FW_CFontRep::FW_CFontRep(FW_CReadableStream& archive) :
- FW_CGraphicCountedPtrRep()
- #ifdef FW_BUILD_MAC
- ,fMacValidCache(FALSE)
- #endif
- {
- FW_CStringArchiver::Read(archive, fFontName);
-
- archive >> fFontStyle;
- archive >> fFontSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::~FW_CFontRep
- //----------------------------------------------------------------------------------------
-
- FW_CFontRep::~FW_CFontRep()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::FW_CFontRep
- //----------------------------------------------------------------------------------------
-
- FW_CFontRep& FW_CFontRep::operator=(const FW_CFontRep& otherRep)
- {
- fFontName = otherRep.fFontName;
- fFontStyle = otherRep.fFontStyle;
- fFontSize = otherRep.fFontSize;
-
- #ifdef FW_BUILD_MAC
- fMacValidCache = FALSE;
- #endif
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::Copy
- //----------------------------------------------------------------------------------------
-
- FW_PFont FW_CFontRep::Copy() const
- {
- FW_PFont font(fFontName, fFontStyle, fFontSize);
- return font;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::SetFontName
- //----------------------------------------------------------------------------------------
-
- void FW_CFontRep::SetFontName(const FW_CString& fontName)
- {
- fFontName = fontName;
- #ifdef FW_BUILD_MAC
- fMacValidCache = FALSE;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::SetFontStyle
- //----------------------------------------------------------------------------------------
-
- void FW_CFontRep::SetFontStyle(FW_FontStyle fontStyle)
- {
- fFontStyle = fontStyle;
- #ifdef FW_BUILD_MAC
- fMacValidCache = FALSE;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CFontRep::Flatten(FW_CWritableStream& archive) const
- {
- FW_CStringArchiver::Write(archive, fFontName);
-
- archive << fFontStyle;
- archive << fFontSize;
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::MacGetFontID
- //----------------------------------------------------------------------------------------
-
- short FW_CFontRep::MacGetFontID() const
- {
- ((FW_CFontRep*)this)->MacCheckFontCache();
- return fMacFontID;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::MacGetFontStyle
- //----------------------------------------------------------------------------------------
-
- Style FW_CFontRep::MacGetFontStyle() const
- {
- ((FW_CFontRep*)this)->MacCheckFontCache();
- return fMacFontStyle;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::MacCheckFontCache
- //----------------------------------------------------------------------------------------
-
- void FW_CFontRep::MacCheckFontCache()
- {
- if (!fMacValidCache)
- {
- fMacFontStyle = normal;
- if (fFontStyle & FW_kBold)
- fMacFontStyle += bold;
- if (fFontStyle & FW_kItalic)
- fMacFontStyle += italic;
- if (fFontStyle & FW_kUnderline)
- fMacFontStyle += underline;
- if (fFontStyle & FW_kStrikeOut)
- fMacFontStyle += normal;
- if (fFontStyle & FW_kOutline)
- fMacFontStyle += outline;
- if (fFontStyle & FW_kShadow)
- fMacFontStyle += shadow;
- if (fFontStyle & FW_kExtended)
- fMacFontStyle += extend;
- if (fFontStyle & FW_kCondensed)
- fMacFontStyle += condense;
-
- // ----- fMacFontID -----
- short fontNumber;
- Str255 pascalFontName;
-
- fFontName.ExportPascal(pascalFontName);
- ::GetFNum(pascalFontName, &fontNumber);
-
- if (fontNumber == systemFont)
- {
- // fontNum is set to sysFont if fontName is not found.
- // We must check for the special case where the system
- // font is indeed being retrieved, by comparing the
- // name of the font with that of the system font.
- Str255 sysFontName;
- ::GetFontName(systemFont, sysFontName);
- if (!::EqualString(pascalFontName, sysFontName, FALSE, FALSE))
- {
- // // font was not found
- // BR_THROW(BR_XFontNameNotFound(fFaceName));
- }
- }
- fMacFontID = fontNumber;
-
- fMacValidCache = TRUE;
- }
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::IsEqual
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CFontRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
- {
- if (other == this)
- return TRUE;
-
- FW_CFontRep* fontRep = FW_DYNAMIC_CAST(FW_CFontRep, other);
- if (fontRep)
- {
- return
- fFontStyle == fontRep->fFontStyle &&
- fFontSize == fontRep->fFontSize &&
- fFontName == fontRep->fFontName;
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::Read
- //----------------------------------------------------------------------------------------
-
- void* FW_CFontRep::Read(FW_CReadableStream& archive)
- {
- FW_CFontRep* objectRep = new FW_CFontRep(archive);
- return objectRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CFontRep::GetFontMetrics
- //----------------------------------------------------------------------------------------
-
- void FW_CFontRep::GetFontMetrics(FW_CGraphicDevice* device, FW_SFontMetrics& fontMetrics) const
- {
- FW_CFontRep* self = (FW_CFontRep*)this;
- FW_PFont tempFont(self);
- device->SelectFont(tempFont, FALSE); // FALSE means no scaling
-
- FW_SFontMetrics::PrivGetFontMetrics(device, fontMetrics);
- }
-